package com.keli.dt.analyzer;

import java.util.ArrayList;
import java.util.List;

import com.keli.dt.model.LaneReport;
import com.keli.dt.model.TravelReport;

public class LaneReportAnalyzer {
	public static List<LaneReport> analyze(List<TravelReport> travelReports) {
		List<LaneReportHelper> helpers = new ArrayList<LaneReportHelper>();
		for (TravelReport tr : travelReports) {
			int index = findHelper(tr.getSBBH(), tr.getDDBH(), tr.getCDBH(),
					tr.getCLLX(), helpers);
			if (index == -1) {
				helpers.add(new LaneReportHelper(tr));
			} else {
				LaneReportHelper helper = helpers.get(index);
				helper.addTravelReport(tr);
				helpers.add(helper);
				helpers.remove(index);
			}
		}
		return toLaneReportList(helpers);
	}

	private static int findHelper(String detectID, String roadId,
			String laneID, String vehicleClass, List<LaneReportHelper> helpers) {
		int index = 0;
		for (LaneReportHelper helper : helpers) {
			if (helper.getDetectID().equals(detectID)
					&& helper.getRoadId().equals(roadId)
					&& helper.getLaneId().equals(laneID)
					&& helper.getVehicleClass().equals(vehicleClass)) {
				return index;
			}
			index++;
		}
		return -1;
	}
	
	private static List<LaneReport> toLaneReportList(List<LaneReportHelper> helpers) {
		List<LaneReport> list = new ArrayList<LaneReport>();
		for (LaneReportHelper helper: helpers) {
			list.add(helper.toLaneReport());
		}
		
		return list;
	}
}
